<?php 
    require_once("./system/loginCheck.php");
    require_once("../congfig/config.php");

    $pName=$_POST["pName"];
    $pChar=$_POST["pChar"];
    $teacher=$_POST["teacher"];
    $pDesc=$_POST["pDesc"];
    $pRank=$_POST["pRank"];
    $pDate=date("Y-m-d");
    $name = $_FILES["Enclosure"]["name"];
    $tid=$_SESSION['loginflag'];


    $sql0="select * from proinfor where pName='$pName'";
    $result0=$conn->query($sql0);
    $num0=$result0->num_rows;
    if ($num0!=0) {
    	echo "项目重名，3秒后返回重新申请";
    	header("refresh:3;url='addProject.php'");
    }
    else{
     

    if ($_FILES["Enclosure"]["error"]) {
      switch ($_FILES["Enclosure"]["error"]) {
        case '1':
            echo "文件尺寸超过了配置文件的最大值";
          # code...
          break;
          case '3':
            echo "部分文件上传";
          # code...
          break;
        case '4':
            echo "没有文件上传";
          # code...
          break;
        
        default:
          echo "未知错误";
          break;
      }
      exit;
    }
    $arr=explode(".",$_FILES["Enclosure"]["name"]);
    $suffix=$arr[count($arr)-1];
    
    $filePath="../teacher/files/";
   
    $randname=date("YmdHis").rand(100,999).".".$suffix;
   if (move_uploaded_file($_FILES["Enclosure"]["tmp_name"], $filePath.$randname)) {
    echo "文件上传成功"."<br>";
    } 
    $sql=" insert into `proinfor`(`id`, `pName`, `pChar`, `teacher`, `pDesc`, `pDate`, `pRank`, `Enclosure`) values (null,'$pName','$pChar','$teacher','$pDesc','$pDate',$pRank,'$randname')";
    $result=$conn->query($sql);
    if ($result==1) {
      $sql1="select * from proinfor where pName='$pName'";
      $result1=$conn->query($sql1);
      $row1=$result1->fetch_array();
      $pid=$row1["id"];
      $sql3="select * from users where username='$pChar'";
      $result3=$conn->query($sql3);
      $row3=$result3->fetch_array();
      $pCharid=$row3["uid"];
      $sql2="insert into `puser`(`id`, `uid`, `pid`) values (null,$tid,$pid),(null,$pCharid,$pid)";
      echo $sql2;
      $result2=$conn->query($sql2);
      if ($result2==1) {
        $text="申请成功!";
        header("location:owner-clareProject.php?text=$text");
      }
      else echo "申请失败";
    	
    }
    else
    	echo "申请失败";

}//end of 重名
?>